package com.gfxy.modules.items.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.modules.common.entity.MemberList;
import com.gfxy.modules.excel.ExcelUtil;
import com.gfxy.modules.items.entity.Zhryl;
import com.gfxy.modules.items.mapper.ZhrylMapper;
import com.gfxy.modules.items.service.IZhrylService;
import com.gfxy.modules.items.vo.ZhrylMemberPage;
import com.gfxy.modules.items.vo.ZhrylPage;
import com.gfxy.modules.items.entity.teachingresult.TeachingResultContent;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.ImportExcelUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 综合荣誉类
 * @Author: jeecg-boot
 * @Date:   2021-10-13
 * @Version: V1.0
 */
@Service
public class ZhrylServiceImpl extends AbstractProjectServiceImpl<ZhrylMapper, Zhryl> implements IZhrylService {
    protected int memberCellNum = 3;

    @Override
    protected void pass(String id, String isPass) {

    }

    @Override
    protected String[] getMemberArray(Row row) {
        String nameStrs = ExcelUtil.formatCell(row.getCell(memberCellNum));
        return nameStrs.split(memberNameSeparator);
    }

    @Override
    protected Zhryl getOneProject(Row row) {
        String date = ExcelUtil.formatCell(row.getCell(0));
        String classess  = ExcelUtil.formatCell(row.getCell(1));
        String projectName = ExcelUtil.formatCell(row.getCell(2));
        Zhryl zhryl = new Zhryl();

        zhryl.setDate(date);
        zhryl.setClassess(translateClassess(classess));
        zhryl.setProjectName(projectName);
        zhryl.setIsPass(0);
        zhryl.setTotalScore(this.getTotalScore(zhryl));
        return zhryl;
    }

    private int translateClassess(String name){
        if(name == null || name.equals("")){
            return 0;
        } else if(name.equals("国家级")){
            return 1;
        } else if(name.equals("省部级")){
            return 2;
        } else if(name.equals("厅局级")){
            return 3;
        } else {
            return 0;
        }
    }

    @Override
    public Double getTotalScore(Zhryl zhryl) {
        double result = 0.0;
        int classess = zhryl.getClassess();
        if(classess == 1){
            result = 300;
        } else if(classess == 2){
            result = 150;
        } else if(classess == 3){
            result = 100;
        }
        return result;
    }

    @Override
    public IPage<ZhrylPage> zhrylPageListByYear(String date, Integer pageNo, Integer pageSize) {
        Page<ZhrylPage> page = new Page<>(pageNo,pageSize);
        return page.setRecords(this.baseMapper.zhrylVoListByYear(date));
    }

    @Override
    public List<ZhrylPage> zhrylPageListByYear(String date) {
        return this.baseMapper.zhrylVoListByYear(date);
    }


    @Override
    protected List<TeachingResultContent> createTeachingResultContent(Zhryl jxcg)  {

        List<TeachingResultContent> result = new ArrayList<>();

        // 类型
        TeachingResultContent classTeachingResultContent = new TeachingResultContent();
        classTeachingResultContent.setType(1);
        classTeachingResultContent.setLevel(jxcg.getClassess());

        try {
            classTeachingResultContent.setValue(dictTranslater.getDictValue(ZhrylMemberPage.class, "classess", String.valueOf(jxcg.getClassess())));
            result.add(classTeachingResultContent);
        } catch (NoSuchFieldException e) {
            log.error(e.getMessage(),e);
        }

        return result;
    }

    @Override
    public Result<?> importExcel(MultipartFile file) throws IOException {
        int SHEET_NUM = 0;
        int errorLines = 0;
        int successLines = 0;
        InputStream inputStream = file.getInputStream();
        if (!(inputStream).markSupported()) {
            inputStream = new PushbackInputStream(inputStream, 8);
        }
        Workbook book = WorkbookFactory.create(inputStream);
        Sheet sheet = book.getSheetAt(SHEET_NUM);
        List<String> errorMessage = new ArrayList<>();

        for (int i = START_ROW_NUM; i <= sheet.getLastRowNum(); i++) {
            ExcelResult excelResult = new ExcelResult(true,"");
            try{

                Row row = sheet.getRow(i);
                Zhryl t = this.getOneProject(row);

                List<MemberList> memberListList = this.getMemberList(row, t, excelResult);

                for (MemberList memberList : memberListList) {
                    List<MemberList> memberLists = new ArrayList<>();
                    memberList.setRanked(1);
                    memberList.setScore(t.getTotalScore());
                    memberLists.add(memberList);
                    this.saveMain(t,memberLists);
                    t.setId(null);
                }
            } catch ( Exception e){
                e.printStackTrace();
                log.error(e.getMessage(),e);
                excelResult.setErrorMsg("excel 第 " + i+ "行数据解析出现位置异常; ");
            }

            if(excelResult.isSuccess()){
                successLines++;
            } else {
                errorLines++;
                errorMessage.add(excelResult.getErrorMsg());
            }

        }

        return ImportExcelUtil.imporReturnRes(errorLines, successLines, errorMessage);
    }
}
